<?php
/**
 * LICENSE
 * 
 * [license information]
 * 
 * @category   BKPay
 * @copyright  Copyright (c) 2011 bkpay.vn
 * @license    http://license.bkpay.vn
 * @version    v 1.0 2011-09-19
 * @author	   hoaihuong@9webdesigns.com, s50702790@gmail.com
 */
require_once 'libs/BKPay/Constant.php';
class BKPay_Security_Client {
	
	public $config;
	/**
	 * Contructor
	 */
	public function __construct() {
		if (is_file('secureconfig.php')) {
			$this->config =  include_once 'secureconfig.php';
		}
	}
	
	/**
	 * Encrypt PKCS7
	 * @param string_type $data : data to encrypt
	 * 
	 */
	
	public function encryptPKCS7($data){
		
		try{
			$key = file_get_contents ( $this->config['myCertFile'] );
			
			/**
			 * Save message to file
			 */
			$fp = fopen ( $this->config['filePath'] . "input.txt", "w" );
			fwrite ( $fp, $data );
			fclose ( $fp );
			
			/**
			 * Encrypt it
			 */
			$header = array ("From" => "bussiness", "To" => "BKPay", "Method" => "Pay" );
			$result = openssl_pkcs7_encrypt($this->config['filePath'] . "input.txt", 
									$this->config['filePath'] . "enc.txt", $key,
		    							$header,PKCS7_BINARY);
		    return $result;
		   
		}catch (Exception $e){
			throw $e;
			return false;
		}
	
	}
	
	/**
	 * 
	 * Sign with mode PKCS7
	 * @param $data : data to sign
	 */
	public function SignPKCS7($data){
	
//		$this->config['passwordKeyFile'] = "bkdragon89";
		$header = array("From" =>"bussiness",
						"To" => "BKPay",
						"Method" => "Pay" );
		try{
			openssl_pkcs7_sign($this->config['filePath'] . "enc.txt", $this->config['filePath'] . "signed.txt", 'file://' . realpath($this->config['myCertFile']), 
								array('file://' . realpath($this->config['myKeyFile']), $this->config['passwordKeyFile']), $header,PKCS7_DETACHED);
		}catch (Exception $e){
			throw $e;
		}
	}
	
	/**
	 * main function to sign and encrypt message
	 */
	public function bkpayEncrypt($form) {
		
		if (! file_exists ($this->config['myKeyFile'] )) {
			echo "ERROR: MY_KEY_FILE: " . $this->config['myKeyFile'] . " not found\n";
		}
		if (! file_exists ( $this->config['myCertFile'] )) {
			echo "ERROR: MY_CERT_FILE: " . $this->config['myCertFile'] . " not found\n";
		}
		if (! file_exists ( $this->config['bkpayCertFile'] )) {
			echo "ERROR: PAYPAL_CERT_FILE: " . $this->config['bkpayCertFile'] . " not found\n";
		}
		
		/**
		 * Format message
		 */
		$data = "";
		foreach ( $form as $key => $value ) {
			if ($value != "") {
				$data .= "$key=$value\n";
			}
		}
		
		//add  md5 of message to data

		//$data = $data."::".md5($data);
		
		//$result = self::encryptPKCS7 ( $data );
		
		$headers = array ("From" => "business", "To" => "BKpay" );
		
		//openssl_pkcs7_sign ( realpath($this->config['filePath'] . "enc.txt"), realpath($this->config['filePath'] . "signed.txt"), 'file://' . realpath($this->config['myCertFile']), array ('file://' . realpath($this->config['myKeyFile']), $this->config['passwordKeyFile'] ), $headers, PKCS7_DETACHED );
		//echo openssl_pkcs7_verify ( $root . "/encrypt/message/signed.txt", PKCS7_DETACHED, $root . "/encrypt/message/output.txt",array($this->config['myCertFile']));
		$this->encryptPKCS7($data);
		
		$this->SignPKCS7($data);
		
		$dataSigned = file_get_contents($this->config['filePath'] . "signed.txt");		
		
		return $dataSigned;
	}
	
}

